//
//  MTNewsContentViewController.m
//  MobTimes
//
//  Created by 江山 王 on 5/12/12.
//  Copyright (c) 2012 Finalist. All rights reserved.
//

#define INDICATOR_TAG                       (1000)

#import "MTStartupsContentViewController.h"

@interface MTStartupsContentViewController ()

@end


@implementation MTStartupsContentViewController

@synthesize newsContentView = _newsContentView;

- (id)initWithNewsContentURLStr:(NSString *)url {
    if (self = [super init]) {
        _newsContentURLStr = [url retain];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)loadView {
    [super loadView];
    
    if (!_newsContentView) {
        _newsContentView = [[UIWebView alloc] init];
        _newsContentView.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height-44);
        _newsContentView.delegate = self;
        [self.view addSubview:self.newsContentView];
    }
    NSURL *_url  = [NSURL URLWithString:_newsContentURLStr];
    NSURLRequest *_request = [NSURLRequest requestWithURL:_url];
    [self.newsContentView loadRequest:_request];
    
    UIActivityIndicatorView *_indicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_TAG];
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicator.frame = CGRectMake((self.view.bounds.size.width-80)/2.0f, (self.view.bounds.size.height-80)/2.0f, 80, 80);
        _indicator.center = CGPointMake(self.view.center.x, self.view.center.y-44-20);
        _indicator.tag = INDICATOR_TAG;
        [self.view addSubview:_indicator];
    }
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [_indicator startAnimating];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (_newsContentView) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        _newsContentView.delegate = nil;
        [_newsContentView loadHTMLString:MT_String("") baseURL:nil];
        [_newsContentView removeFromSuperview];
        [_newsContentView release];
        _newsContentView = nil;
    }
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [_newsContentURLStr release];
    _newsContentURLStr = nil;
    self.newsContentView = nil;
    [super dealloc];
}

#pragma mark - UIWebViewDelegate methods implementation

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    UIActivityIndicatorView *_indicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_TAG];
    if (_indicator) {
        [_indicator stopAnimating];
    }
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

@end
